Schema Management PRO
This section explains how to create, modify, and remove tables and indexes using SQLite’s structured schema APIs.
Instead of relying solely on raw SQL strings, Schema Management provides a declarative, readable, and safer approach to defining database structures, making it suitable for long-term, maintainable data models.
Creating Tables
createTable
createTable creates a new table.
Example:
ColumnDefinition
Common column attributes include:
name: column nametype: SQLite column type (for example,INTEGER,TEXT)primaryKey: whether the column is a primary keyautoIncrement: enables auto-increment (only valid for integer primary keys)notNull: applies a NOT NULL constraintunique: applies a UNIQUE constraintindexed: creates an index on the columncheckSQL: CHECK constraint expressioncollation: column collation ruledefaultValue: default value (parameterized form)defaultSQL: default value (raw SQL expression)references: foreign key reference definition
Default Values
defaultValue and defaultSQL are mutually exclusive. Use one or the other.
Foreign Key References
Example:
Renaming Tables
renameTable
Example:
Dropping Tables
dropTable
Example:
Creating Indexes
createIndex
Example:
Partial Indexes
Dropping Indexes
dropIndex
Example:
dropIndexOn
Drops the index associated with the specified table and column combination.
Example:
Design Principles
The Schema Management API follows these principles:
-
Structure over string concatenation Reduces the risk of SQL syntax errors and improves readability
-
Declarative over imperative Clearly describes what the schema should be, not how to assemble SQL
-
One-to-one mapping with SQLite capabilities Avoids hidden behavior or unexpected abstractions
Usage Recommendations
- Prefer
createTablefor long-lived schemas - Use
ifNotExiststo avoid duplicate creation - Create indexes explicitly for frequently queried columns
- Ensure
foreignKeysEnabledis enabled when using foreign keys - Perform schema changes within transactions or migration logic
Next Steps
After creating and managing schemas, you may want to:
- Inspect existing database structures
- Retrieve column, primary key, and foreign key information
- Analyze indexes and constraints
Continue with:
- Schema Introspection
